
package com.wuye.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;

import com.wuye.common.dao.hibernate.BaseDaoHibernate;
import com.wuye.common.util.string.StrUtil;
import com.wuye.common.vo.CommonObjectVO;
import com.wuye.common.vo.PageInfo;
import com.wuye.constants.BaseConstants;
import com.wuye.dao.RoomDao;
import com.wuye.entity.Room;
import com.wuye.entity.RoomPartyRel;
import com.wuye.mapper.CommonMapper;
import com.wuye.util.MyBatisSessionFactory;

@Repository(value = "roomDao")
public class RoomDaoImpl extends BaseDaoHibernate implements RoomDao {

	public PageInfo getRoom(Map<String, Object> map, int currentPage,
			int perPageNum) {
		if (map == null) {
			return null;
		}
		String qryType = StrUtil.strnull(map.get("qryType"));
		StringBuffer hql = new StringBuffer();
		String fuzzy = (String) map.get("fuzzy");
		// String buildingName = (String) map.get("buildingName");
		Integer buildingId = (Integer) map.get("buildingId");
		Integer communityId = (Integer) map.get("communityId");
		Integer companyId = (Integer) map.get("companyId");
		Integer roomId = (Integer)map.get("roomId");
		String account = StrUtil.strnull(map.get("account"));
		String roomNbr = StrUtil.strnull(map.get("roomNbr"));
		List<Object> params = new ArrayList<Object>();
		if ("all".equals(qryType)) {
			if (roomId != null && !roomId.equals(0)){
				hql.append("select a from Room a where a.roomId = ? ");
				params.add(roomId);
			}else if (buildingId != null && !buildingId.equals(0)) {
				hql.append("select a from Room a where a.building.buildingId = ? ");
				params.add(buildingId);
			}else if (communityId != null && !communityId.equals(0)) {
				hql.append("select a from Room a, Building b ")
						.append(" where 1=1 ").append(" and a.building = b")
						.append(" and b.community.communityId=?");
				params.add(communityId);
			} else {
				hql.append("select a from Room a where 1=1 ");
			}
			
			if (!StrUtil.isNullOrEmpty(roomNbr)){
				hql.append(" and a.roomNbr=? ");
				params.add(roomNbr);
			}
		}else if ("byAccount".equals(qryType)){
			if (!StrUtil.isNullOrEmpty(account)){
				if (communityId != null && !communityId.equals(0)){
					hql.append(" select a from Room a,Building b1,RoomPartyRel b,Party c,User d ")
					.append(" where 1=1 ")
					.append(" and a.building = b1 ")
					.append(" and b1.community.communityId= ? ");
					params.add(communityId);
				}else if (companyId != null && !companyId.equals(0)){
					hql.append(" select a from Room a,Building b1,Community c1,RoomPartyRel b,Party c,User d ")
					.append(" where 1=1 ")
					.append(" and a.building = b1  ")
					.append(" and b1.community = c1 ")
					.append(" and c1.propertyCompany.companyId = ? ")
					;
					params.add(companyId);
				}else{
					hql.append(" select a from Room a,RoomPartyRel b,Party c,User d ");
				}
				hql.append(" and a = b.room")
				.append(" and b.partyInfo = c")
				.append(" and c.objId = d.userId ")
				.append(" and b.relType = ? ")
				.append(" and c.classId = ? ")
				.append(" and d.account = ? ");
				params.add(BaseConstants.ROOM_PARTY_REL_TYPE_10);
				params.add(BaseConstants.CLASS_USER);
				params.add(account);
			}
			
		}else if ("checkExist".equals(qryType)){
			hql.append("select a from Room a where a.building.buildingId = ? and a.roomNbr = ? ");
			params.add(buildingId);
			params.add(StrUtil.strnull(map.get("roomNbr")));
		}

		PageInfo pageInfo = super.findPageInfoByHQLAndParams(hql.toString(),
				params, currentPage, perPageNum);
		return pageInfo;
	}

	public RoomPartyRel getOwnerRel(Room room) {
		StringBuffer hql = new StringBuffer();
		hql.append("select rp from RoomPartyRel rp");
		// hql.append(" where rp.room.roomId = r.roomId ");
		hql.append(" where rp.relType= ? ");
		hql.append(" and rp.statusCd=? ");
		hql.append(" and rp.room.roomId = ? ");
		List<Object> params = new ArrayList<Object>();
		params.add(BaseConstants.ROOM_PARTY_REL_TYPE_10);
		params.add(BaseConstants.STATUS_VALID);
		params.add(room.getRoomId());
		List<RoomPartyRel> rels = super.findListByHQLAndParams(hql.toString(),
				params);
		if (rels == null || rels.size() == 0) {
			return null;
		}
		return rels.get(0);
	}

	public List<CommonObjectVO> getRoomInfo(int unitId) 
	{
		List<CommonObjectVO> roomInfos = null;
		SqlSession session = null;
		
		try 
		{
			session = MyBatisSessionFactory.getSession();			
			CommonMapper commonMapper = session.getMapper(CommonMapper.class);
			roomInfos = commonMapper.getRoomInfo(unitId);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			session.close();
		}
		
		return roomInfos;
	}

	public Room getRoom(int roomId) 
	{
		return (Room)super.getObject(Room.class, roomId);
	}
}
